package com.minicrm.web.controller.system;

import java.util.Date;
import java.util.List;

import com.minicrm.common.utils.DateUtils;
import com.minicrm.common.utils.ShiroUtils;
import com.minicrm.system.domain.Borrow;
import com.minicrm.system.service.IBorrowService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.minicrm.common.annotation.Log;
import com.minicrm.common.enums.BusinessType;
import com.minicrm.system.domain.BootInfo;
import com.minicrm.system.service.IBootInfoService;
import com.minicrm.common.core.controller.BaseController;
import com.minicrm.common.core.domain.AjaxResult;
import com.minicrm.common.utils.poi.ExcelUtil;
import com.minicrm.common.core.page.TableDataInfo;

/**
 * 【请填写功能名称】Controller
 * 
 * @author minicrm
 * @date 2022-03-21
 */
@Controller
@RequestMapping("/system/info")
public class BootInfoController extends BaseController
{
    private String prefix = "system/info";

    @Autowired
    private IBootInfoService bootInfoService;

    @Autowired
    private IBorrowService borrowService;

    @RequiresPermissions("system:info:view")
    @GetMapping()
    public String info()
    {
        return prefix + "/info";
    }

    /**
     * 查询列表
     */
    @RequiresPermissions("system:info:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BootInfo bootInfo)
    {
        startPage();
        List<BootInfo> list = bootInfoService.selectBootInfoList(bootInfo);
        return getDataTable(list);
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @RequiresPermissions("system:info:export")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BootInfo bootInfo)
    {
        List<BootInfo> list = bootInfoService.selectBootInfoList(bootInfo);
        ExcelUtil<BootInfo> util = new ExcelUtil<BootInfo>(BootInfo.class);
        return util.exportExcel(list, "【请填写功能名称】数据");
    }

    /**
     * 新增【请填写功能名称】
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存
     */
    @RequiresPermissions("system:info:add")
    @Log(title = "增加图书信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BootInfo bootInfo)
    {
        return toAjax(bootInfoService.insertBootInfo(bootInfo));
    }

    /**
     * 修改
     */
    @RequiresPermissions("system:info:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        BootInfo bootInfo = bootInfoService.selectBootInfoById(id);
        mmap.put("bootInfo", bootInfo);
        return prefix + "/edit";
    }

    /**
     * 修改
     */
    @RequiresPermissions("system:info:borrow")
    @GetMapping("/borrow/{id}")
    public String editBorrow(@PathVariable("id") Long id, ModelMap mmap)
    {
        BootInfo bootInfo = bootInfoService.selectBootInfoById(id);
        mmap.put("bootInfo", bootInfo);
        return prefix + "/bootInfo";
    }

    /**
     * 借书
     */
    @RequiresPermissions("system:info:borrow")
    @Log(title = "借书", businessType = BusinessType.UPDATE)
    @PostMapping("/borrowBook")
    @ResponseBody
    public AjaxResult borrowBook(BootInfo bootInfo)
    {
        bootInfo = bootInfoService.selectBootInfoById(bootInfo.getId());
        bootInfo.setCount(bootInfo.getCount()-1);
        int count = bootInfoService.updateBootInfo(bootInfo);
        //增加借阅记录信息
        Borrow borrow = new Borrow();
        borrow.setBorrowDate(DateUtils.getNowDate());
        borrow.setUserId(ShiroUtils.getSysUser().getUserId());
        borrow.setBorrowId(bootInfo.getId());
        borrowService.insertBorrow(borrow);
        return toAjax(count);
    }

    /**
     * 修改保存【请填写功能名称】
     */
    @RequiresPermissions("system:info:edit")
    @Log(title = "编辑图书信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BootInfo bootInfo)
    {
        return toAjax(bootInfoService.updateBootInfo(bootInfo));
    }

    /**
     * 删除【请填写功能名称】
     */
    @RequiresPermissions("system:info:remove")
    @Log(title = "删除图书信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(bootInfoService.deleteBootInfoByIds(ids));
    }
}
